package com.miao.util.string;

import com.miao.util.exception.MyException;
import org.apache.commons.lang.StringUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class BankStringUtil {

    public static final String ZEROFORMAT = "-";
    public static final String DOUBLEFORMAT = "0.00";
    public static final String DATEFORMAT = "yyyy年MM月";

    /**
     * 截取字符串返回Integer数组
     *
     * @param originalString
     * @param character
     * @return
     */
    // 没做参数判空
    //存在非数字字符串抛异常的可能,要处理
    public static Integer[] subStringToInt(String originalString, char character) throws Exception {

        // 如果originalString串中包含character
        if (StringUtils.contains(originalString, character)) {
            originalString = originalString.trim();
            String[] childString = originalString.split(character + "");
            Integer[] childInteger = new Integer[childString.length];
            for (int i = 0; i < childString.length; i++) {
                //存在非数字字符串抛异常的可能,要处理
                try {
                    childInteger[i] = Integer.parseInt(childString[i]);
                } catch (NumberFormatException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return childInteger;
        } else {
            return null;
        }
    }

    /**
     * 返回指定格式的浮点数的字符串
     *
     * @param num
     * @param formatStr     例:"0.00"
     * @param zeroFormatStr
     * @return
     */
    public static String doubleNumericDisp(double num, String formatStr, String zeroFormatStr) {
        if (num != 0.0)
            return new DecimalFormat(formatStr).format(num);
        else
            return zeroFormatStr;
    }

    /**
     * 返回指定格式的日期型数的字符串
     *
     * @param date
     * @param formatStr 例:"yyyy年MM月"
     * @return
     */
    public static String dateNumericDisp(Date date, String formatStr) {
        return new SimpleDateFormat(formatStr).format(date);
    }

    /**
     * 传入字符串类型的日期是否满足日期条件
     *
     * @return
     */
    public static String getMeededStringDate(String arg) throws Exception {
        int year = 0, month = 0, days = 0;
        try {
            // 这个日期格式是否合理符合yyyyMMdd?? 要测试
            if (arg.matches("\\d+\\.?\\d*")) {
                if (arg.length() != 8) {
                    return null;
                }
                year = Integer.parseInt(arg.substring(0, 4));
                month = Integer.parseInt(arg.substring(4, 6));
                days = Integer.parseInt(arg.substring(6, 8));
            } else // 针对yyyy-mm-dd,是否合理,要测试
                if (arg.length() == 10) {
                    year = Integer.parseInt(arg.substring(0, 4));
                    month = Integer.parseInt(arg.substring(5, 7));
                    days = Integer.parseInt(arg.substring(8, 10));
                }
        } catch (Exception e) {
            return null;
        }

        // 加个位前导0
        if (year > 1900 && year < 2100 && month > 0 && month < 13 && days > 0 && days < 32) {
            String a = year + "";
            if (month < 10) {
                a += "0";
            }
            a += month;
            if (days < 10) {
                a += "0";
            }
            a += days;
            return a;
        }
        return null;
    }

    /**
     * 字符串转换数字,出错抛异常
     *
     * @param str
     * @return
     */
    public static Integer str2int(String str) throws MyException {
        Integer retValue = -9999;
        if (StringUtils.isBlank(str)) {
            throw new MyException("字符串为空,无法转换成数字");
        }
        try {
            retValue = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            throw new MyException(str + "不是数字形式,无法转换成数字");
        }
        return retValue;
    }

    public static final void main(String[] args) {
        System.out.println(doubleNumericDisp(44000000.33, "##", "0.0"));
        //System.out.println(doubleNumericDisp(44000000.33,"##0.00","0.0"));

        try {
            System.out.println(str2int("dfdf"));
        } catch (MyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
